from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login

def login_view(request):
    return render(request, 'login.html')


def login_user(request):
    if request.method != 'POST':
        return redirect('/login')

    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
        login(request, user)
        # TODO Jump to Previous URL
        # nexturl = request.GET['next']
        # if len(nexturl>0):
        #     return redirect(nexturl)
        # else:
        return redirect('/feed/')
    else:
        return redirect('/login')

